import JayUtil from "../../Fantasy/Util/JayUtil";
import ServerCenter from "../../Server/ServerCenter";
import QuestSpawn from "../QuestSpawn";
import SoundUtil from "../../Fantasy/Util/SoundUtil";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MacthQuestJs extends cc.Component {
    @property(cc.Node)
    questNodeArr:Array<cc.Node> = [];
    @property(cc.Node)
    answerNode:cc.Node = null;
    @property(cc.Prefab)
    answerpre:cc.Prefab = null;
    questType;
    questArr;
    answerIndex;
    gameJs;
    questExp;
    arr = [,,,];
    answer={
        '0':'',
        '1':'',
        '2':'',
        '3':'',
    };
    onEnable(){
        Log("正确答案",this.arr)
        if(QuestSpawn.isWrong == true){
            for(let i=0;i<this.questNodeArr.length;i++){
                this.questNodeArr[i].getChildByName("quest").active = false;
                this.questNodeArr[i].getChildByName("answer").getComponent(cc.Label).string = this.arr[i];
                this.questNodeArr[i].getChildByName("answer").active = true;
            }
        }
    }

    init(nodeJs,wordsList){
        this.gameJs = nodeJs;
        this.questType = Math.floor(Math.random()*2)
        if(this.questType == 0){
            this.setWordsQuest(wordsList);//看英文选中文
        }else{
            this.setTranslateQuest(wordsList);//看中文选英文
        }
        this.answerIndex = 0;
        this.questExp = 5;
 

    }
    setWordsQuest(wordsList){
        this.questArr = this.getAnserWords(wordsList,4);
        for(let i=0; i<this.questArr.length;i++){
            this.questNodeArr[i].getChildByName("quest").getComponent(cc.Label).string = this.questArr[i].words;
            this.arr[i] = this.questArr[i].translation;
        }
        var answerArr = JayUtil.shuffle(this.questArr);
        for(let i=0; i<answerArr.length;i++){
            let node = cc.instantiate(this.answerpre);
            node.getComponent("MatchButtonJs").setLabel(answerArr[i].translation);
            // node.getChildByName("label").getComponent(cc.Label).string = answerArr[i].translation;
            node.parent = this.answerNode;
        }

    }

    setTranslateQuest(wordsList){
        this.questArr = this.getAnserWords(wordsList,4);
        for(let i=0; i<this.questArr.length;i++){
            this.questNodeArr[i].getChildByName("quest").getComponent(cc.Label).string = this.questArr[i].translation;
            this.arr[i] = this.questArr[i].words;
        }
        var answerArr = JayUtil.shuffle(this.questArr);
        for(let i=0; i<answerArr.length;i++){
            let node = cc.instantiate(this.answerpre);
            node.getComponent("MatchButtonJs").setLabel(answerArr[i].words);
            // node.getChildByName("label").getComponent(cc.Label).string = answerArr[i].words;
            node.parent = this.answerNode;
        }
    }
    
    getAnserWords(wordsList,number){
        if(number>Object.keys(wordsList).length){
           return;
        }   
        var tempArr=wordsList.slice(0);
        var newArrList=[];    
        for(var i=0;i<number;i++){
            var random=Math.floor(Math.random()*(tempArr.length-1));
            var arr=tempArr[random];
            tempArr.splice(random, 1);
            newArrList.push(arr);    
        }
        return JayUtil.shuffle(newArrList);

    }

    updateAnswerIndex(){
        Log(this.answer);
        for(let i =0;i<Object.keys(this.answer).length;i++){
            if(this.answer[i] == ""){
                this.answerIndex = i;
                return;
            }
        }
    }

    setAnswer(label){
        this.answer[this.answerIndex] = label;
        this.questNodeArr[this.answerIndex].getChildByName("quest").active = false;
        this.questNodeArr[this.answerIndex].getChildByName("answer").getComponent(cc.Label).string = label;
        this.questNodeArr[this.answerIndex].getChildByName("answer").active = true;
        this.updateAnswerIndex();
    }

    cancelAnswer(label){
        for(let i =0;i<Object.keys(this.answer).length;i++){
            if(label == this.answer[i]){
                this.answer[i] = "";
                this.questNodeArr[i].getChildByName("quest").active = true;
                this.questNodeArr[i].getChildByName("answer").active = false;
                this.updateAnswerIndex();
                return;
            }
        }
    }

    checkAnswer(){
        if(QuestSpawn.isWrong == true){
            return;
        }
        var number = 0;
        Log(this.questType == 0);
        if(this.questType == 0){
            for(let i =0;i<Object.keys(this.answer).length;i++){
                if(this.answer[i] == this.questArr[i].translation){
                    number++;
                }
            }
        }else{
            for(let i =0;i<Object.keys(this.answer).length;i++){
                if(this.answer[i] == this.questArr[i].words){
                    number++;
                }
            }
        }
        if(number == 4){
            SoundUtil.playEffect("music/correct");
            this.gameJs.updateExp(this.questExp);
            this.gameJs.initQuest();
            Log("恭喜你");
        }else{
            SoundUtil.playEffect("music/wrong");
            if(QuestSpawn.isContest == true){
                this.gameJs.updateExp(0,false)
                this.gameJs.initQuest();
            }else{
                if(this.gameJs.updateLife){
                    this.gameJs.updateLife();
                }
                this.gameJs.updateExp(0,false);
            }
            Log("很遗憾哦");
        }
    }



}
